package com.jyxd.web.service.dictionary;

import com.jyxd.web.dao.basic.RespiratorBoxDictonaryDao;
import com.jyxd.web.data.dictionary.RespiratorBoxDictonary;
import com.jyxd.web.data.dto.HttpResult;
import com.jyxd.web.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.jyxd.web.util.ExecuteUtil.errorIfEmpty;
import static com.jyxd.web.util.ExecuteUtil.isEmpty;

/**
 * 呼吸机盒子字典设置service
 */

@Service
@Transactional
public class RespiratorBoxDictonaryService {

    @Autowired
    private RespiratorBoxDictonaryDao respiratorBoxDictonaryDao;

    public boolean insert(RespiratorBoxDictonary respiratorBoxDictonary) {
        return respiratorBoxDictonaryDao.insert(respiratorBoxDictonary);
    }

    public boolean update(RespiratorBoxDictonary respiratorBoxDictonary) {
        return respiratorBoxDictonaryDao.update(respiratorBoxDictonary);
    }

    public RespiratorBoxDictonary queryData(String id) {
        return respiratorBoxDictonaryDao.queryData(id);
    }

    public List<RespiratorBoxDictonary> queryList(Map<String, Object> map) {
        return respiratorBoxDictonaryDao.queryList(map);
    }

    public int queryNum(Map<String, Object> map) {
        return respiratorBoxDictonaryDao.queryNum(map);
    }

    /**
     * 通过呼吸机编号清空数据
     *
     * @param respiratorId
     */
    public boolean cleanByRespiratorId(String respiratorId) {
        return respiratorBoxDictonaryDao.cleanByRespiratorId(respiratorId);
    }

    /**
     * 通过盒子编号跟新呼吸机编号
     *
     * @param respiratorId
     * @param boxNumbers
     */
    public boolean updateByRespiratorId(String respiratorId, String boxNumbers) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("respiratorId", respiratorId);
        map.put("boxNumbers", boxNumbers);
        return respiratorBoxDictonaryDao.updateByRespiratorId(map);
    }

    public HttpResult saveOrUpdate(RespiratorBoxDictonary respiratorBoxDictonary) {
        try {
            errorIfEmpty(respiratorBoxDictonary, "参数不能为空");
            if (isEmpty(respiratorBoxDictonary.getId())) {
                respiratorBoxDictonary.setId(UUIDUtil.getUUID());
                respiratorBoxDictonary.setCreateDate(new Date());
                insert(respiratorBoxDictonary);
            } else
                update(respiratorBoxDictonary);
            return new HttpResult(200, "修改成功");
        } catch (Exception e) {
            return new HttpResult(500, e.getMessage());
        }
    }

    public int deleteById(String id) {
        return respiratorBoxDictonaryDao.deleteById(id);
    }
}
